#include <cstdio>
#include <cstring>
#include <cmath>

#define L 1000005
const int MAX=2140000000;
int n;
int a[L],queue[L];
int main()
{
	int i,k,head,tail;
	scanf("%d%d",&n,&k);
	for(i=1;i<=n;i++)
		scanf("%d",&a[i]);
	queue[1]=0;head=1;tail=1;
	a[0]=MAX;
	for(int i=1;i<=n;i++)
	{
		while(tail>0&&a[i]<=a[queue[tail]])tail--;
		tail++;
		queue[tail]=i;
		if(tail<head)head=tail;
		while(i-queue[head]>=k)head++;
		if(i>=k)printf("%d ",a[queue[head]]);
		}
	printf("\n");
	memset(queue,0,sizeof(queue));
	queue[1]=0;head=1;tail=1;
	a[0]=-MAX;
	for(int i=1;i<=n;i++)
	{
		while(tail>0&&a[i]>=a[queue[tail]])tail--;
		tail++;
		queue[tail]=i;
		if(tail<head)head=tail;
		while(i-queue[head]>=k)head++;
		if(i>=k)printf("%d ",a[queue[head]]);
	}
	printf("\n");

	return 0;
}

